package com.demo.myapplication.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

import com.demo.myapplication.R;
import com.demo.myapplication.bean.ADEntity;
import com.demo.myapplication.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

public class ADTextView extends TextView {
  private static final String TAG = "ADTextView";

  private Paint mPaintFront;
  private Paint mPaintBack;

  private int mIndex; //当前显示item的index

  private List<ADEntity> mTexts = new ArrayList<>();

  public ADTextView(Context context) {
    this(context, null);
  }

  public ADTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public ADTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
  }

  private void init(AttributeSet attrs) {
    TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.ADTextView);
    int mFrontColor = ta.getColor(R.styleable.ADTextView_text_front_color, Color.RED);
    int mBackColor = ta.getColor(R.styleable.ADTextView_text_back_color, Color.BLACK);
    ta.recycle();

    mPaintFront = initPaint(mFrontColor);
    mPaintBack = initPaint(mBackColor);

    mIndex = 0;
  }

  private Paint initPaint(int color) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(color);
    paint.setTextSize(CommonUtils.getDensity() * 18);
    return paint;
  }

  private Paint initPaint() {
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.GREEN);
    return paint;
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = measureWidth(widthMeasureSpec);
    int height = measureHeight(heightMeasureSpec);

    mHeight = height;
    mWidth = width;
    Log.d(TAG, "onMeasure: mHeight=" + mHeight + ",mWidth=" + mWidth);
    setMeasuredDimension(width, height);
  }

  //设置一个回调并设置setXXX方法
  public interface OnClickListener {
    void onClick(String mUrl);
  }

  private OnClickListener clickListener;

  public void setOnClickListener(OnClickListener clickListener) {
    this.clickListener = clickListener;
  }

  //重写onTouchEvent事件,并且要返回true,表明当前的点击事件由这个组件自身来处理
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    switch (action) {
      case MotionEvent.ACTION_UP:
        if (clickListener != null) {
          //调用回调,将当前数据源的链接传出去
          clickListener.onClick(mTexts.get(mIndex).getmUrl());
        }
        break;
      default:
        break;
    }
    return true;
  }

  //测量宽度
  private int measureHeight(int heightMeasureSpec) {
    int result;
    int mode = MeasureSpec.getMode(heightMeasureSpec);
    int size = MeasureSpec.getSize(heightMeasureSpec);

    if (mode == MeasureSpec.EXACTLY) {
      result = size;
    } else { //高度至少为两倍字高
      int mfronTextHeight = (int) (mPaintFront.descent() - mPaintFront.ascent()); //前缀文字字高
      int mContentTextHeight = (int) (mPaintBack.descent() - mPaintBack.ascent()); //内容文字字高
      result = Math.max(mfronTextHeight, mContentTextHeight) * 2;

      if (mode == MeasureSpec.AT_MOST) {
        result = Math.min(result, size);
      }
    }
    return result;
  }

  //测量高度
  private int measureWidth(int widthMeasureSpec) {
    int result;
    int mode = MeasureSpec.getMode(widthMeasureSpec);
    int size = MeasureSpec.getSize(widthMeasureSpec);
    if (mode == MeasureSpec.EXACTLY) {
      result = size;
    } else { //宽度最小十个字的宽度
      String text = "十个字十个字十个字字";
      Rect rect = new Rect();
      mPaintBack.getTextBounds(text, 0, text.length(), rect);
      result = rect.right - rect.left;
      if (mode == MeasureSpec.AT_MOST) {
        result = Math.min(result, size);
      }
    }
    return result;
  }

  public void setTexts(List<ADEntity> text) {
    this.mTexts.clear();
    this.mTexts.addAll(text);
    mIndex = 0;
    postInvalidate();
  }

  private int mHeight = 0;
  private int mWidth = 0;


  private int mY;
  private boolean mInit = true;
  private boolean isPause = false;

  @Override
  protected void onDraw(Canvas canvas) {
    if (mTexts != null && mTexts.size() > 0) {

      ADEntity entity = mTexts.get(mIndex);
      String frontText = entity.getmFront();
      String backText = entity.getmBack();

      Rect frontRect = new Rect();
      mPaintFront.getTextBounds(frontText, 0, frontText.length(), frontRect);

      Rect backRect = new Rect();
      mPaintBack.getTextBounds(backText, 0, backText.length(), backRect);

      //第一次，初始化
      //最下面
      if (mInit && mIndex == 0) {
        mY = mHeight - backRect.top;
        mInit = false;
      }

      //最上面，重新初始为最下面
      if (mY <= -backRect.bottom) {
        mIndex++;
        mY = mHeight - backRect.top;
        isPause = false;
      }

      //重头
      if (mIndex >= mTexts.size()) {
        mIndex = 0;
        mY = mHeight - backRect.top;
        isPause = false;
      }

      canvas.drawText(frontText, 10, mY, mPaintFront);
      canvas.drawText(backText, frontRect.right - frontRect.left + 20, mY, mPaintBack);
//      Paint paint = initPaint();
//      paint.setColor(Color.BLACK);
//      canvas.drawLine(50, 0, 50, mHeight, initPaint());
//      canvas.drawLine(0, mY, mWidth, mY, paint);
      Rect rect = new Rect(0, 0, mWidth, mHeight);
      canvas.drawRect(rect, initPaint());
//      canvas.drawLine(0, mHeight / 2, mWidth, mHeight / 2, initPaint());
      mY -= 1;

      //中间
      if (!isPause && mY <= getMeasuredHeight() / 2 - (frontRect.top + frontRect.bottom) / 2) {
        //暂停
        isPause = true;
        postInvalidateDelayed(2000);
      } else {
        //滑动
        postInvalidateDelayed(2);
      }
      mY -= 1;
    }
  }
}
